package com.ch.base.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.ch.base.util.StringUtils;

public class PaginationBodyTag extends TagSupport{
	private final Logger log = Logger.getLogger(this.getClass());

	private String displaySpace;
	private String paginationType;
	private String preActionUrl;
	private String actionUrl;
	private String targetObjId;
	private String returnChkId;
	private String chkAllObjId;
	private String chkObjName;
	private int totalSize;
	private int pageSize;
	private int currentPage;
	private int totalPage;
	private int startPageNo;
	private int endPageNo;
	
	public int doStartTag() throws JspException{
		JspWriter out = pageContext.getOut();
		try{
			totalSize = (Integer) pageContext.getRequest().getAttribute("totalSize");
			pageSize = (Integer) pageContext.getRequest().getAttribute("pageSize");
			currentPage = (Integer) pageContext.getRequest().getAttribute("currentPage");
			totalPage = (Integer) pageContext.getRequest().getAttribute("totalPage");
			
			startPageNo = (currentPage - 1) * pageSize + 1;
			endPageNo = startPageNo + pageSize - 1;
			if(endPageNo > totalSize){
				endPageNo = totalSize;
			}
			
//			log.info("totalSize = " + totalSize);
//			log.info("pageSize = " + pageSize);
//			log.info("currentPage = " + currentPage);

			if(displaySpace.equals("start")){
				out.println("<script>");
				out.println("$(function() {");
				if(paginationType.equals("normal")){
					out.println("	$(\"#" + targetObjId + " tr[name='tr_" + targetObjId + "']\").hide();");
					
					out.println("	var url = String(location);");
					out.println("	var paginationPage_" + targetObjId + "  = \"paginationPage_" + targetObjId + "\"");
					out.println("	var paginationSize_" + targetObjId + " = \"paginationSize_" + targetObjId + "\"");
					out.println("	var newPage = \"1\";");
					out.println("	var newPageSize = \"" + pageSize + "\";");
					out.println("	if (url.indexOf(paginationPage_" + targetObjId + ") > 0){ ");
					out.println("		var currentUrl = parseURL(url);");
					out.println("		for (var x in currentUrl.hash) {");
					out.println("			if (paginationPage_" + targetObjId + " == x) {");
					out.println("				newPage = currentUrl.hash[x] ;");
					out.println("				break;");
					out.println("			}");
					out.println("		}");
					out.println("		for (var x in currentUrl.hash) {");
					out.println("			if (paginationSize_" + targetObjId + " == x) {");
					out.println("				newPageSize = currentUrl.hash[x] ;");
					out.println("				break;");
					out.println("			}");
					out.println("		}");
					out.println("	}");
					
//					out.println("	goToNextPage('" + paginationType+ "', null, null, '" + targetObjId + "', 'firstPage', " + totalSize + ", " + pageSize + ", null, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');");
					out.println("	goToNextPage('" + paginationType+ "', null, null, '" + targetObjId + "', 'numberPage', " + totalSize + ", newPageSize, newPage, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');");
				}
				out.println("	$('#" + targetObjId + "').css('display', '');");
				out.println("});");
				out.println("</script>");

				out.println("<input type=\"hidden\" id=\"pageSize_" + targetObjId + "\" name=\"pageSize\" value=\""+pageSize+"\" />");
				out.println("<input type=\"hidden\" id=\"currentPage_" + targetObjId + "\" name=\"currentPage\" value=\""+currentPage+"\" />");
				out.println("<input type=\"hidden\" id=\"totalSize_" + targetObjId + "\" name=\"totalSize\" value=\""+totalSize+"\" />");
			}
		}catch(Exception ex){
			throw new JspException(ex);
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try{
			out.println(this.getPaginationcontent());
		}catch(Exception ex){
			throw new JspException(ex);
		}
		this.release();
		return EVAL_PAGE;
	}

	public String getPaginationcontent() throws JspException {
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"pagination\" style=\"float:right;\">");
		sb.append("	<span><span name=\"totalSize\">" + totalSize + "</span> 個中的第 <span name=\"startPageNo\">" + startPageNo + "</span> - <span name=\"endPageNo\">" + endPageNo + "</span> 個</span>");
		sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', '" + preActionUrl + "', '" + actionUrl + "', '" + targetObjId + "', 'firstPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), null, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" title=\"第一頁\">&laquo; 第一頁</a>");
		sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', '" + preActionUrl + "', '" + actionUrl + "', '" + targetObjId + "', 'prevPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), null, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" title=\"前一頁\">&lsaquo; 前一頁</a>");
		sb.append("	<span name=\"numberLink\">");
		//		sb.append("		<s:iterator begin=\"1\" end=\"5\">");
		//		sb.append("			<a href=\"javascript:goToNextPage('" + paginationType + "', null, '" + targetObjId + "', 'numberPage', <s:property value=\"queryList2.size()\"/>, $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), <s:property value=\"top\" />, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" <s:if test=\"currentPage != top\">class=\"number\"</s:if><s:else>class=\"number current\"</s:else> title=\"<s:property value = \"top\"/>\"><s:property value = \"top\"/></a>");
		//		sb.append("		</s:iterator>");
		sb.append("	</span>");
		sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', '" + preActionUrl + "', '" + actionUrl + "', '" + targetObjId + "', 'nextPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), null, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" title=\"下一頁\">下一頁 &rsaquo;</a>");
		sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', '" + preActionUrl + "', '" + actionUrl + "', '" + targetObjId + "', 'lastPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), null, " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" title=\"最後一頁\">最後一頁 &raquo;</a>");
		sb.append("	<span name=\"totalSize\">" + totalSize + "</span> 筆資料，");
		sb.append("	共 <span name=\"totalPage\">" + totalPage + "</span> 頁，");
		sb.append("	前往第");
		sb.append("	<input type=\"text\" id=\"currentPage" + StringUtils.proper(displaySpace, null) + "_" + targetObjId + "\" name=\"currentPage\" size=\"1\" value=\"" + currentPage + "\" />");

		//		if(displaySpace.equals("start")){
		//			sb.append("	<input type=\"text\" id=\"currentPage" + StringUtil.proper(displaySpace, "") + "_" + targetObjId + "\" name=\"currentPage\" size=\"1\" />");
		//		}else if(displaySpace.equals("end")){
		//			sb.append("	<input type=\"text\" id=\"currentPageEnd_" + targetObjId + "\" name=\"currentPage\" size=\"1\" />");
		//		}
		sb.append("	頁 ");
		sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', '" + preActionUrl + "', '" + actionUrl + "', '" + targetObjId + "', 'numberPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), $('#" + targetObjId + " #currentPage" + StringUtils.proper(displaySpace, null) + "_" + targetObjId + "').val(), " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" class=\"button\">確定</a>");

		//		if(displaySpace.equals("start")){
		//			sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', null, '" + targetObjId + "', 'numberPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), $('#" + targetObjId + " #currentPageStart_" + targetObjId + "').val(), " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" class=\"button\">確定</a>");
		//		}else if(displaySpace.equals("end")){
		//			sb.append("	<a href=\"javascript:goToNextPage('" + paginationType + "', null, '" + targetObjId + "', 'numberPage', " + totalSize + ", $('#" + targetObjId + " #pageSize_" + targetObjId + "').val(), $('#" + targetObjId + " #currentPageEnd_" + targetObjId + "').val(), " + returnChkId + ", '" + chkAllObjId + "', '" + chkObjName + "');\" class=\"button\">確定</a>");
		//		}
		sb.append("</div>&nbsp;");
		return sb.toString();
	}

	public String getDisplaySpace() {
		return displaySpace;
	}

	public void setDisplaySpace(String displaySpace) {
		this.displaySpace = displaySpace;
	}

	public String getPaginationType() {
		return paginationType;
	}

	public void setPaginationType(String paginationType) {
		this.paginationType = paginationType;
	}

	public String getPreActionUrl() {
		return preActionUrl;
	}

	public void setPreActionUrl(String preActionUrl) {
		this.preActionUrl = preActionUrl;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}

	public String getTargetObjId() {
		return targetObjId;
	}

	public void setTargetObjId(String targetObjId) {
		this.targetObjId = targetObjId;
	}

	public String getReturnChkId() {
		return returnChkId;
	}

	public void setReturnChkId(String returnChkId) {
		this.returnChkId = returnChkId;
	}

	public String getChkAllObjId() {
		return chkAllObjId;
	}

	public void setChkAllObjId(String chkAllObjId) {
		this.chkAllObjId = chkAllObjId;
	}

	public String getChkObjName() {
		return chkObjName;
	}

	public void setChkObjName(String chkObjName) {
		this.chkObjName = chkObjName;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public Logger getLog() {
		return log;
	}

	
}
